//
//  RankListTableViewController.m
//  老司机发车了
//
//  Created by SuperNing on 16/8/6.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "RankListTableViewController.h"

#import "RankListOneModel.h"
#import "RankListTwoModel.h"
#import "RankListTableViewCell.h"

#import "DetailSongTableViewController.h"

@interface RankListTableViewController ()

@property (nonatomic, strong) NSMutableArray *mainListArray;

@property (nonatomic, strong) NSMutableArray *contentArray;

@end

@implementation RankListTableViewController

- (NSMutableArray *)mainListArray{
    
    if (!_mainListArray) {
        _mainListArray = [[NSMutableArray alloc] init];
    }
    return _mainListArray;
}

- (NSMutableArray *)contentArray
{
    if (!_contentArray) {
        _contentArray = [[NSMutableArray alloc] init];
    }
    return _contentArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self analyseData];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"RankListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    blurView.frame = self.view.bounds;

    self.tableView.backgroundView = blurView;
    
}

- (void)analyseData{
    
    [NetWorkRequestManager requestWithType:POST urlString:RankList_Url parDic:@{} finish:^(NSData *data) {
        
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        
        for (NSDictionary *dict in dataDic[@"content"]) {
            
            RankListOneModel *model = [[RankListOneModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *dict1 in dict[@"content"]) {
                RankListTwoModel *contentModel = [[RankListTwoModel alloc] init];
                [contentModel setValuesForKeysWithDictionary:dict1];
                [tempArray addObject:contentModel];
            }
            [self.contentArray addObject:tempArray];
            model.content = tempArray;
            [self.mainListArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
           
            [self.tableView reloadData];
        });
        
    } finishError:^(NSError *error) {
        
    }];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _mainListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RankListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    RankListOneModel *oneModel = self.mainListArray[indexPath.row];
    [cell.picView sd_setImageWithURL:[NSURL URLWithString:oneModel.pic_s192]];
    cell.titleLabel.text = oneModel.name;
    
    RankListTwoModel *model1 = self.contentArray[indexPath.row][0];
    cell.OneLabel.text = [NSString stringWithFormat:@"%@ - %@",model1.title,model1.author];

    RankListTwoModel *model2 = self.contentArray[indexPath.row][1];
    cell.twoLabel.text = [NSString stringWithFormat:@"%@ - %@",model2.title,model2.author];

    RankListTwoModel *model3 = self.contentArray[indexPath.row][2];
    cell.threeLabel.text = [NSString stringWithFormat:@"%@ - %@",model3.title,model3.author];

    RankListTwoModel *model4 = self.contentArray[indexPath.row][3];
    cell.fourLabel.text = [NSString stringWithFormat:@"%@ - %@",model4.title,model4.author];

    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailSongTableViewController *detailVC = [[DetailSongTableViewController alloc] init];
    RankListOneModel *model = self.mainListArray[indexPath.row];
    detailVC.type = [NSString stringWithFormat:@"%ld",(long)model.type];
    detailVC.picUrl = model.pic_s192;
    detailVC.jsonNum = 1;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
